package org.csu.jpetstore_front.service.alipay.config;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.csu.jpetstore_front.service.alipay.bean.AlipayBean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Component
public class Alipay {

    private static final String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    private static final String appId = "2021000119664862";
    //应用私钥
    private static final String privateKey = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCK4nJA6m9c12MFJNx6dIDEfn2Yx08VIrbd9wxngEhLONQhhcdkfAoz0qr08kzYcc5d0DylAlb3jpIUQpUO0haeo//BNnBN98EumZ0LqgbZvtUWIfS0AFJ6N+jLrJrp6vksiGrcfphyUZR8JssRjteRqH2rDxW+7UKwZ2QZFfmdmtqXB74UhizxZiQYSu86W/hOWTH11iheHatDl6tgTjipaDEvosu9er97ogD4UsPnF4F6wHVcO8o9cFLwbMlFnsvzZ5Wc+iTuTVkIWoWlgjUEd9wxgQD+XW9/wAv1JQhEo0sBphJTsQdEvaAdfdPamF+EKoWsOYbK/hJEIcBSHQ+rAgMBAAECggEBAINSxbguNKj0HPRTO4squEuUU4xwAY45qfJOVNNVUIaYMLWV+slAJKGF9RLyPcl4Kys36PAaO0OP8v7nSmyeKLdO2VnH8P84Ytl+dSn/VsT0Fib8apb9qv9X+G8GQeEnxv7d6zWdGucVzrJqOB/dEmbvjAyAkg5b6HBHAzZZOhNUYOMh3AeEJBXH4InYMYXh9fSqi5xu3tgEQk80xxYXS+EIbobMcjvL1DzQByDdRrgIvTxZaxdN6SL1vJpapMspNGCHXGFilyUhQdIgltIf6S9W14+k0AN+Jlm3T+o8JNsIMii8ZJRGZ1DKzVsgpxKtcGgAz6cOvrPoM5lls6D8wekCgYEAz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BoWt2s/uCHUO+O6xNJvbQa8Y2V6lQS2fmacDBaJFetYrUAuCNow/yspSfquY4PRJlqUFUEqsciJk6JiOV3vlUPA1asCsRgrB7pSggjJmEjC6e5gYoKF5rxR5Acv++vqAPXCX634q6X/WJ8Q1PaDwUDnHDW4/m6r7W+KvUQukIFpWV3Fy1OTMolz1OJ6IwV2k4dvHL8myJr+cjoEcVwIDAQAB";
    private static final String format = "json";
    private static final String charset = "UTF-8";
    //支付成功要异步调用的地方
    private static final String notifyUrl = "http://localhost:8080/orders/paySuccess";
    //支付成功要去往的地方
    private static final String returnUrl = "http://localhost:8080/order/paySuccess";

    private static final String signType = "RSA2";

    public String pay(AlipayBean alipayBean) throws AlipayApiException {


        // 1、获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, publicKey, signType);
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 3、页面跳转同步通知页面路径
//        alipayRequest.setReturnUrl(returnUrl);
        // 4、服务器异步通知页面路径(没用,不设置了)
        alipayRequest.setNotifyUrl(notifyUrl);
        // 5、封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 6、请求支付宝进行付款，并获取支付结果
        return alipayClient.pageExecute(alipayRequest).getBody();

    }


}
